package dancrawler.server;

import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.util.LinkedList;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 *
 */
public class Connection {
    private LinkedList<netMsg> messages;
    private LinkedList<netMsg> messagesOut;
    private LinkedList<ByteBuffer> outb;
    private String id;
    private SelectionKey key;
    private BlockingQueue<netMsg> outQueue = new LinkedBlockingQueue<>();
    private Socket socket;
    private long lastPing = 0;

    public String getId() {
        return id;
    }
    
    public long getLastPing()
    {
        return lastPing;
    }
    public void setLastPing(long lp)
    {
        lastPing = lp;
    }
    public BlockingQueue<netMsg> getOutQueue()
    {
        return outQueue;
    }
    
    public void setKey(SelectionKey k)
    {
        key = k;
    }
    public SelectionKey getKey()
    {
        return key;
    }

    public void setId(String id) {
        this.id = id;
    }
    
    public Connection()
    {
        messages = new LinkedList<>();
        outb = new LinkedList<>();
    }
    public void setSocket(Socket sock)
    {
        socket = sock;
    }
    public Socket getSocket()
    {
        return socket;
    }
    public void addOutb(ByteBuffer buff)
    {
        outb.add(buff);
    }
    public ByteBuffer peekOutb()
    {
        return outb.peek();
    }
    public void removeOutb()
    {
        outb.remove();
    }
    public void addOutMsg(netMsg msg)
    {
        messagesOut.add(msg);
    }
    public netMsg getMsgOut()
    {
        return messagesOut.peek();
    }
    public netMsg removeMsgOut()
    {
        return messagesOut.pop();
    }
    public void addMsg(netMsg msg)
    {
        messages.add(msg);
    }
    public void removeMsg(netMsg msg)
    {
        messages.remove(msg);
    }
    public netMsg popMsg()
    {
        if(messages.size() > 0 && messages.peekFirst().isFinalized())
        {
            return messages.pop();
        }
        else
        {
            return null;
        }
    }
    public netMsg peekMsg()
    {
        return messages.peekLast();
    }
    
}
